﻿using Newtonsoft.Json;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using 植物大战僵尸杂交版数据联网.ExtUrls;
using 植物大战僵尸杂交版数据联网.Models;

namespace 植物大战僵尸杂交版数据联网.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "植物大战僵尸杂交版联网";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
        private string userName;

        public string UserName
        {
            get { return userName; }
            set { userName = value; RaisePropertyChanged(); }
        }
        private string selectUserName;

        public string SelectUserName
        {
            get { return selectUserName; }
            set { selectUserName = value; RaisePropertyChanged(); }
        }

        private int maxValue;

        public int MaxValue
        {
            get { return maxValue; }
            set { maxValue = value; RaisePropertyChanged(); }
        }
        private int minValue=0;

        public int MinValue
        {
            get { return minValue; }
            set { minValue = value; RaisePropertyChanged(); }
        }
        private int proValue;

        public int ProValue
        {
            get { return proValue; }
            set { proValue = value; RaisePropertyChanged(); }
        }
        private string stateText;

        public string StateText
        {
            get { return stateText; }
            set { stateText = value; RaisePropertyChanged(); }
        }

        public ObservableCollection<string> UserList { get; set; }
        public DelegateCommand DownloadCommand { get; set; }
        public DelegateCommand RefreshCommand { get; set; }
        public DelegateCommand UploadCommand { get; set; }
        public DelegateCommand StartCommand { get; set; }

        public MainWindowViewModel()
        {
            UserList = new ObservableCollection<string>();
            Initial();

            DownloadCommand = new DelegateCommand(async () =>
            {
                try
                {
                    string url = await 获取最新下载连接();
                }
                catch (Exception)
                {
                    MessageBox.Show("无法下载！请尝试开启国外代理下载！");
                }
            });

            UploadCommand = new DelegateCommand(async () =>
            {
                try
                {
                    string json = await HttpHelper.PostJson($"{ExtUrl.UploadUrl}?DirName={UserName}", ExtUrl.Host);
                    MessageBox.Show("上传完成");

                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message);
                }
            });

            RefreshCommand = new DelegateCommand(async () =>
            {
                await Initial();
            });

            StartCommand = new DelegateCommand(async () =>
            {
                if (string.IsNullOrEmpty(SelectUserName))
                {
                    MessageBox.Show("请选择同步的用户资源");
                    return;
                }
               string json=  await HttpHelper.GetJson($"{ExtUrl.GetFileUrl}?DirName={SelectUserName}",ExtUrl.Host);
                GetFilesJson.Rootobject root= JsonConvert.DeserializeObject<GetFilesJson.Rootobject>(json);

                MaxValue = root.msg.Length;
                MinValue = 0;
                ProValue = 0;
                foreach (var item in root.msg)
                {
                    StateText = $"正在下载【{item}】";
                    await HttpHelper.DownLoad($"{SelectUserName}/{item}",ExtUrl.SaveData);
                    ProValue++;
                }

                MessageBox.Show("同步完成");

                StateText = "";
                ProValue = 0;
            });
        }

        private async Task Initial()
        {
            UserList.Clear();

            UserName = Environment.UserName;

            string fileJson = await HttpHelper.GetJson(ExtUrl.GetDirNameUrl, ExtUrl.Host);
            var dirName = JsonConvert.DeserializeObject<GetDirNameJson.Rootobject>(fileJson);
            dirName.msg.ToList().ForEach(x => UserList.Add(x));
        }

        public async Task<string> 获取最新下载连接()
        {
            //color=\"slate\" href=\"(https://r2.182666.xyz(.*?).zip)

            string html = await HttpHelper.GetJson("https://pvzgame.net/");
            Regex regex = new Regex("color=\"slate\" href=\"(https://r2.182666.xyz(.*?).zip)");
            return regex.Match(html).Groups[1].Value.Trim();
        }
    }
}
